package ua.kpi.ipsa.expert.api.db;

import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.DatabaseEntry;

public class AlgorithmTuple {

	public int algorithmPK;
	public String className;
	public String name;

	public double aa11;
	public double aa21;
	public double aa22;

	public double af1;
	public double af2;

	public int userOpinionPositiveCount;
	public int userOpinionMeasurementCount;
	public int code1Count;
	public int code1PlagiarismCount;

	public AlgorithmTuple() {
	}

	public AlgorithmTuple(int algorithmPK) {
		this.algorithmPK = algorithmPK;
	}

	AlgorithmTuple(DatabaseEntry key, DatabaseEntry data) {
		setKeyEntry(key);
		setValueEntry(data);
	}

	DatabaseEntry getKeyEntry() {
		DatabaseEntry entry = new DatabaseEntry();
		IntegerBinding.intToEntry(algorithmPK, entry);
		return entry;
	}

	void setKeyEntry(DatabaseEntry entry) {
		algorithmPK = IntegerBinding.entryToInt(entry);
	}

	DatabaseEntry getValueEntry() {
		DatabaseEntry entry = new DatabaseEntry();
		TupleOutput out = new TupleOutput();
		out.writeString(className);
		out.writeString(name);
		out.writeDouble(aa11);
		out.writeDouble(aa21);
		out.writeDouble(aa22);
		out.writeDouble(af1);
		out.writeDouble(af2);
		out.writeInt(userOpinionPositiveCount);
		out.writeInt(userOpinionMeasurementCount);
		out.writeInt(code1Count);
		out.writeInt(code1PlagiarismCount);
		entry.setData(out.getBufferBytes(), out.getBufferOffset(),
				out.getBufferLength());
		return entry;
	}

	void setValueEntry(DatabaseEntry entry) {
		TupleInput in = new TupleInput(entry.getData(), entry.getOffset(),
				entry.getSize());
		className = in.readString();
		name = in.readString();
		aa11 = in.readDouble();
		aa21 = in.readDouble();
		aa22 = in.readDouble();
		af1 = in.readDouble();
		af2 = in.readDouble();
		userOpinionPositiveCount = in.readInt();
		userOpinionMeasurementCount = in.readInt();
		code1Count = in.readInt();
		code1PlagiarismCount = in.readInt();
	}
}